package at.ac.tuwien.vpsbcm.spielzeugfabrik.alternativ.arbeiter;

import java.util.Random;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

import at.ac.tuwien.vpsbcm.spielzeugfabrik.alternativ.teile.Holzgehaeuse;
import at.ac.tuwien.vpsbcm.spielzeugfabrik.alternativ.teile.Lokomotive;
import at.ac.tuwien.vpsbcm.spielzeugfabrik.alternativ.teile.Schornstein;

public class Lackierer extends Arbeiter implements Runnable, MessageListener {
    
    private static int lackiererId;
      
    private TopicSubscriber topicSubscriber;
    private TopicPublisher topicPublisher;
    private Lokomotive lokomotive = null;
      
    public Lackierer() throws JMSException {
        super();

        topicSubscriber = getLokTopicQueueConnHandler().getTopSubscriber();
        topicSubscriber.setMessageListener(this);
        topicPublisher = getLokTopicQueueConnHandler().getTopPublisher();
    }
    
    private synchronized TopicPublisher getTopicPublisher() {
        return this.topicPublisher;
    }
     
    private synchronized void publish() throws JMSException {
        ObjectMessage messageOut = getTopicSession().createObjectMessage();
        messageOut.setStringProperty("bauteil","Lokomotive");
        messageOut.setObject(lokomotive);
        topicPublisher.publish(messageOut);
        resetLokomotiveAvailability();
    }
    
    private  synchronized TopicSession getTopicSession() {
        return super.topicSession;
    }
    
    private synchronized void resetLokomotiveAvailability() {
        lokomotive = null;
    }
    
    private synchronized boolean isLokomotiveAvailable() {
        return lokomotive != null;
    }
    
    private synchronized boolean isLokomotiveLackiert() {
        return lokomotive.isLackiert();
    }
    
    public static void main(String[] args) {
        try {
            Random randomGenerator = new Random();
            lackiererId = randomGenerator.nextInt(100);
            Lackierer lackierer = new Lackierer();
            Thread t = new Thread(lackierer);
            t.start();
        } catch (JMSException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    
    public void run() {
        System.out.println("Ich bin ein Lackierer mit der Nummer: " + lackiererId);
        while (true) {
            if (isLokomotiveAvailable()) {
                if (isLokomotiveLackiert() == true) {
                    try {
                        System.out.println("Lackierer -> Die Lok wird versendet!");
                        publish();
                    } catch (JMSException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    public void onMessage(Message message) {
        ObjectMessage oMsg = null;
        try {
            if (message instanceof ObjectMessage) {
                oMsg = (ObjectMessage) message;
                if (oMsg.getStringProperty("bauteil").equals("Schornstein")) {
                    System.out.println("Lackierer -> Reading message: "
                            + ((Schornstein) oMsg.getObject()).getName()
                            + ((Schornstein) oMsg.getObject()).getId());
                } else if (oMsg.getStringProperty("bauteil").equals("Holzgehaeuse")) {
                    System.out.println("Lackierer -> Reading message: "
                            + ((Holzgehaeuse) oMsg.getObject()).getName()
                            + ((Holzgehaeuse) oMsg.getObject()).getId());
                } else if (oMsg.getStringProperty("bauteil").equals("Lokomotive")) {
                    lokomotive = (Lokomotive) oMsg.getObject();
                    if (lokomotive.isLackiert() == true) {
                        System.out.println("Lackierer -> Die Lok ist bereits lackiert!");
                    } else {
                        System.out.println("Lackierer -> Reading message: "
                                + ((Lokomotive) oMsg.getObject()).getName()
                                + ((Lokomotive) oMsg.getObject()).getId());
                    }
                    lokLackieren(lokomotive);
                }
            } else {
                System.out.println("Message of wrong type: " + message.getClass().getName());
            }
        } catch (JMSException e) {
            System.out.println("JMSException in onMessage(): " + e.toString());
        } catch (Throwable t) {
            System.out.println("Exception in onMessage():" + t.getMessage());
        }
    }
    
    
    public void lokLackieren(Lokomotive lok) {
        lok.setLackiert(true);
    }
    
    public void schornsteinLackieren(Schornstein schornstein) {
        
    }
    
    public void gehaeuseLackieren(Holzgehaeuse holzgehaeuse) {
        
    }
    

}
